﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZeroBug.EasyFlow.Core;

namespace ZeroBug.EasyFlow.Step
{
    public class UpdateVariableStep : IStep
    {
        public bool IsSingleRun { get; set; }
        public bool IgnoreChildren { get; set; }
        public string Icon { get; set; } = "Colours";
        public int ChildrenIndex { get; set; }
        public Core.Step? NextStep { get; set; }

        VariableExpression variableName;
        VariableExpression updateIndex;
        VariableExpression variableValue;
        public bool Compile(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = string.Empty;
            return step.CheckParameters(out errInfo, "VariableName", "UpdateIndex", "VariableValue");
        }

        public object GetForm()
        {
            throw new NotImplementedException();
        }

        public StepInfo GetInfo()
        {
            StepInfo info = new StepInfo();
            info.StepName = "UpdateVariable";
            info.Group = StepGroup.Variable;
            info.ImagePath = "pack://application:,,,/ZeroBug.EasyFlow.Step;component/Images/条件.png";
            return info;
        }

        public void OnChildrenEnter()
        {

        }

        public void OnChildrenLeave()
        {

        }

        public bool Prepare(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = string.Empty;
            return true;
        }

        public void RegisterVariables(FlowBase flow, Core.Step step)
        {

        }

        public StepResult Run(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = string.Empty;

            Variable varName = step.Parameters["VariableName"].GetEquivalent();
            Variable index = step.Parameters["UpdateIndex"].GetEquivalent();
            Variable varValue = step.Parameters["VariableValue"].GetEquivalent();
            
            for (int i = 0; i < varName.Count; i++)
            {
                Variable vs = flow.GetFlowManager().VariableManager.GetVariable(varName[i]);
                for (int j = 0; j < index.Count; j++)
                {
                    vs[Convert.ToInt32(index[j])] = varValue[j];
                }
            }

            return StepResult.Success;
        }

        public string GetTip(Core.Step step)
        {
            return "";
        }

        public (SerializableDictionary<string, VariableExpression>, List<VariableJudge>) GetParametersAndConditions()
        {
            SerializableDictionary<string, VariableExpression> paras = new SerializableDictionary<string, VariableExpression>();
            paras.Add("VariableName", new VariableExpression("VariableName"));
            paras.Add("UpdateIndex", new VariableExpression("UpdateIndex"));
            paras.Add("VariableValue", new VariableExpression("VariableValue"));

            return (paras, new List<VariableJudge>());
        }

        public void UnRegisterVariables(FlowBase flow, Core.Step step)
        {
        }
    }
}
